package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPatronsSkinBond;
import com.yanqu.road.entity.player.UserSkin;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class UserPatronsSkinBondDaoImpl extends TempDataObjectDao<UserPatronsSkinBond> {
    public static String getUserPatronsSkinBondSql = "select * from `t_u_patrons_skin_bond` where `userId`=?";
    public Map<Integer, UserPatronsSkinBond> getUserPatronsSkinBond(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserPatronsSkinBond> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserPatronsSkinBondSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPatronsSkinBond data = parse(rs);
                dataMap.put(data.getCombId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserPatronsSkinBondSql = "update `t_u_patrons_skin_bond` set level=?,bondIdSet=? where userId=? and combId=?";
    @Override
    protected boolean updateData(UserPatronsSkinBond userPatronsSkinBond) {
        userPatronsSkinBond.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userPatronsSkinBond.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userPatronsSkinBond.getBondIdSet())));
        para.put(i++, new DbParameter(Types.BIGINT, userPatronsSkinBond.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userPatronsSkinBond.getCombId()));
        if (getDb().executeNoneQuery(updateUserPatronsSkinBondSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserPatronsSkinBondSql = "insert into `t_u_patrons_skin_bond`(userId,combId,level,bondIdSet) values (?,?,?,?)";
    @Override
    protected boolean addData(UserPatronsSkinBond userPatronsSkinBond) {
        userPatronsSkinBond.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userPatronsSkinBond.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userPatronsSkinBond.getCombId()));
        para.put(i++, new DbParameter(Types.INTEGER, userPatronsSkinBond.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userPatronsSkinBond.getBondIdSet())));
        if (getDb().executeNoneQuery(insertUserPatronsSkinBondSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserPatronsSkinBond parseData(ResultSet rs) throws SQLException {
        UserPatronsSkinBond userPatronsSkinBond = new UserPatronsSkinBond();
        userPatronsSkinBond.setUserId(rs.getLong("userId"));
        userPatronsSkinBond.setCombId(rs.getInt("combId"));
        userPatronsSkinBond.setLevel(rs.getInt("level"));
        userPatronsSkinBond.setBondIdSet(JSONObject.parseObject(rs.getString("bondIdSet"), new TypeReference<ConcurrentHashSet<Integer>>(){}));

        return userPatronsSkinBond;
    }
}
